package com.sx.zongzhi.presenter;

import com.sx.zongzhi.MyApplication;
import com.sx.zongzhi.bean.PhotoBean;
import com.sx.zongzhi.bean.ServiceObjectBean;
import com.sx.zongzhi.bean.ServiceRecordDetailBean;
import com.sx.zongzhi.bean.ServicerBean;
import com.sx.zongzhi.modle.ApiManager;
import com.sx.zongzhi.net.BaseCallBack;
import com.sx.zongzhi.net.BaseResult;
import com.sx.zongzhi.view.IServiceRecordDetailView;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by XZY on 2017/11/30.
 *获取服务记录详情
 */
public class ServiceRecordDetailPest {

    private IServiceRecordDetailView detailView;

    public ServiceRecordDetailPest(IServiceRecordDetailView detailView) {
        this.detailView=detailView;
    }

    /**
     * 获取详亲
     * @param id 服务记录主键id
     */
    public void getServiceRecordDetail(String id){
        Map<String,Object> map=new HashMap<>();
        map.put("sid", MyApplication.sid);
        map.put("id", id);
        new ApiManager().getServiceRecordDetail(map, new BaseCallBack<BaseResult<ServiceRecordDetailBean>>() {
            @Override
            public void onSuccess(BaseResult<ServiceRecordDetailBean> response) {
                detailView.getServiceRecordDetail(response.getData());
            }
            @Override
            public void onFailures(String message) {
                detailView.showError(message);
            }
        });
    }

    /**
     * 添加服务记录
     * @param addServiceRecordCode 新增类型编号。10:文字、30：录音、50：视频、70：相册。编号为10时，不能上传附件，编号为30、50、70时附件必填
     * @param occurDate 服务时间(yyyy-MM-dd)
     * @param occurPlace 服务地点
     * recordType 记录类型(0:排查类,1:整改类) 全部默认0
     * @param serviceJoiners 其他服务人员
     * @param serviceContent 服务内容
     * @param serviceMembers 服务人员
     * @param serviceObjects 服务对象
     * @param photoBeen 上传文件
     */
    public void addServiceRecord(String addServiceRecordCode, String occurDate, String occurPlace,
                                 String serviceJoiners, String serviceContent,
                                 ServicerBean.RowsBean serviceMembers,
                                 ServiceObjectBean.RowsBean serviceObjects,
                                 String objectMoudle,String objectType,
                                 List<PhotoBean> photoBeen){
        Map<String,Object> map=new HashMap<>();
        map.put("sid", MyApplication.sid);
        map.put("serviceRecord.addServiceRecordCode", addServiceRecordCode);
        map.put("serviceRecord.occurDate", occurDate);
        map.put("serviceRecord.occurPlace", occurPlace);
        map.put("serviceRecord.recordType", 0);
        map.put("serviceRecord.serviceJoiners", serviceJoiners);
        map.put("serviceRecord.serviceContent", serviceContent);
        if (serviceMembers!=null){
            map.put("serviceRecord.serviceMembers", serviceMembers.getMemberName());
            map.put("serviceRecord.baseId", serviceMembers.getMemberBaseId());
            map.put("serviceRecord.teamId", serviceMembers.getTeamId());
            map.put("serviceRecord.teamName", serviceMembers.getTeamName());
        }
        if (serviceObjects!=null){
            map.put("serviceRecord.serviceObjects", serviceObjects.getObjectName());
            map.put("serviceRecord.objectId", serviceObjects.getObjectId());
        }
        map.put("serviceRecord.objectMoudle", objectMoudle);
        map.put("serviceRecord.objectType", objectType);
        if (photoBeen!=null&&photoBeen.size()>0){
            for(int i=0;i<photoBeen.size();i++){
                map.put("serviceRecord.files["+i+"].path", photoBeen.get(i).getPath());
                map.put("serviceRecord.files["+i+"].suffixType", photoBeen.get(i).getSuffixType());
            }
        }
        new ApiManager().addServiceRecord(map, new BaseCallBack<BaseResult<ServiceRecordDetailBean>>() {
            @Override
            public void onSuccess(BaseResult<ServiceRecordDetailBean> response) {
                detailView.getServiceRecordDetail(response.getData());
            }
            @Override
            public void onFailures(String message) {
                detailView.showError(message);
            }
        });
    }


}
